package com.cykj.auth.controller;

import com.cykj.auth.form.LoginBody;
import com.cykj.auth.service.ShopLoginService;
import com.cykj.common.core.domain.R;
import com.cykj.common.core.utils.JwtUtils;
import com.cykj.common.core.utils.StringUtils;
import com.cykj.common.security.auth.AuthUtil;
import com.cykj.common.security.service.TokenService;
import com.cykj.common.security.utils.SecurityUtils;
import com.cykj.system.api.model.BsLoginUser;
import com.cykj.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Program: Cykj-Order-System
 * @Package: com.cykj.auth.controller
 * @Author: 张俊杰
 * @Create: 2025-08-22 00:03
 * @Description: 商家登录注册等功能
 * @Vision: 1.0
 **/
@RestController
@RequestMapping("/bsUser")
public class BsTokenController {

    @Autowired
    private ShopLoginService shopLoginService;

    @Autowired
    private TokenService tokenService;

    @PostMapping("/login")
    public R<?> login(@RequestBody LoginBody form) {
        // 用户登录
        BsLoginUser bsLoginUser = shopLoginService.login(form.getUsername(), form.getPassword());
        // 获取登录token
        return R.ok(tokenService.createTokenShop(bsLoginUser));
    }

    @DeleteMapping("/logout")
    public R<?> logout(HttpServletRequest request) {
        String token = SecurityUtils.getToken(request);
        if (StringUtils.isNotEmpty(token)) {
            // 删除用户缓存记录
            AuthUtil.logoutByToken(token);
        }
        return R.ok();
    }


}
